﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shove.Web.UI;

using System.Text;

public partial class UserControls_GoodDetail : UCBase
{
    public string StringBuilder = "";

    public UserControls_GoodDetail()
    {
        Name = "商品详细信息";

        swpas = new ShoveWebPartAttribute[2];

        swpas[0] = new ShoveWebPartAttribute_Int("产品图片宽度", "1,300", 300, "产品图片宽度");
        swpas[1] = new ShoveWebPartAttribute_Int("产品图片高度", "1,300", 280, "产品图片高度");

        //swpas[2] = new ShoveWebPartAttribute_Int("导航单元格宽度", "1,600", 409, "导航单元格宽度");
        //swpas[3] = new ShoveWebPartAttribute_Color("控件边框颜色", "#E7C890", "控件边框颜色");
        //swpas[4] = new ShoveWebPartAttribute_Image("导航背景图片1", "", "images/UserControls/title_bg_hui.jpg", "导航背景图片1");
        //swpas[5] = new ShoveWebPartAttribute_Image("导航背景图片2", "", "images/UserControls/title_bg_hui.jpg", "导航背景图片2");
        //swpas[6] = new ShoveWebPartAttribute_Text("导航字体样式", "", "black12", "导航字体样式");

        //swpas[9] = new ShoveWebPartAttribute_Int("产品名行高", "1,300", 30, "产品名行高");
        //swpas[10] = new ShoveWebPartAttribute_Text("产品图片样式", "", "photo", "产品图片样式");
        //swpas[11] = new ShoveWebPartAttribute_Text("产品名称样式", "", "td14", "产品名称样式");
        //swpas[12] = new ShoveWebPartAttribute_Text("产品编号样式", "", "help12", "产品编号样式");
        //swpas[13] = new ShoveWebPartAttribute_Text("产品简介样式", "", "help12", "产品简介样式");
        //swpas[14] = new ShoveWebPartAttribute_Text("价格的CSS样式", "", "black12", "价格的CSS样式");
        //swpas[15] = new ShoveWebPartAttribute_Text("产品详细CSS样式", "", "black12", "产品详细CSS样式");

        //swpas[16] = new ShoveWebPartAttribute_Image("立即购买图片", "", "images/UserControls/button_car_5.jpg", "立即购买图片");
        //swpas[17] = new ShoveWebPartAttribute_Image("加入收藏图片", "", "images/UserControls/button_car_7.jpg", "加入收藏图片");
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        long GoodID = Shove._Convert.StrToLong(Shove._Web.Utility.GetRequest("ID"), 0);

        if (_Customer == null)
        {
            this.pComment.Visible = false;
            this.pCommentLogin.Visible = true;
            this.lbShowInfo1.Visible = false;
            this.lbShowInfo2.Visible = false;
            this.pConsultLogin.Visible = true;
            this.pConsult.Visible = false;
        }
        else
        {
            //是否已经评论
            DataTable dt = new DAL.Tables.tbl_Site_GoodComment().Open("", "CustomerID=" + _Customer.ID + " and ShopStockID=" + this.hShopStockID.Value, "");

            //是否购买
            DataTable dtBuy = Shove.Database.MSSQL.Select("SELECT * FROM dbo.tbl_Shop_SalesDetail WHERE SalesID IN( SELECT ID FROM dbo.tbl_Shop_SalesMaster WHERE CustomerID = " + _Customer.ID + ") AND GoodID = " + GoodID.ToString());

            this.pComment.Visible = false;
            this.pCommentLogin.Visible = false;
            this.lbShowInfo1.Visible = true;
            this.lbShowInfo2.Visible = true;

            if (dtBuy != null && dtBuy.Rows.Count > 0 && !(dt != null && dt.Rows.Count > 0))
            {
                this.pComment.Visible = true;
                this.pCommentLogin.Visible = false;
                this.lbShowInfo1.Visible = false;
                this.lbShowInfo2.Visible = false;
            }

            this.pConsultLogin.Visible = false;
            this.pConsult.Visible = true;
        }
        
        BindData();
        BindGoodComment();
        BindGoodConsult();
    }

    /// <summary>
    /// 加载商品详细信息
    /// </summary>
    private void BindData()
    {
        long ShopID = _Site.ID;
        long ShopStockID = Shove._Convert.StrToLong(Shove._Web.Utility.GetRequest("ID"), 0);

        DataTable dt = new DAL.Views.V_Site_GoodShopStock().Open("", "ID=" + ShopStockID.ToString(), "");

        if (dt == null || dt.Rows.Count == 0)
        {
            return;
        }

        DataRow dr = dt.Rows[0];

        //该产品为百企商品还是会员推荐商品
        hShopID.Value = dr["ShopID"].ToString();
        lbName_CN.Text = dr["GoodName_CN"].ToString();
        lbGoodNo.Text = dr["GoodNo"].ToString();
        lbBrand.Text = dr["BrandName_CN"].ToString();
        lbType.Text = dr["TypeName"].ToString();
        lbName_CN.Text = dr["GoodName_CN"].ToString();
        lbName_EN.Text = dr["GoodName_EN"].ToString();
        //lbBV.Text = dr["Price_BV"].ToString();
        lbUnit.Text = dr["GoodUnit"].ToString();
        lbIntroduce.Text = dr["GoodIntroduce"].ToString();
        lbOrigPrice.Text = dr["Price_Orig"].ToString();
        lbVIPPrice.Text = dr["Price_Vip"].ToString();
        lbCost.Text = dr["Cost"].ToString();
        lbDetail.Text = dr["GoodIntroduce_Detail"].ToString();

        //lbColor.Text = dr["GoodColor"].ToString();
        //lbNorm.Text = dr["GoodNorm"].ToString();
        string[] Norm = dr["GoodNorm"].ToString().Split('&');
        string[] Color = dr["GoodColor"].ToString().Split('&');

        int length = Norm.Length;
        for (int i = 0; i < length; i++)
        {
            ddlNorm.Items.Insert(i, Norm[i]);
        }

        length = Color.Length;
        for (int i = 0; i < length; i++)
        {
            ddlColor.Items.Insert(i, Color[i]);
        }

        this.hID.Value = dr["GoodID"].ToString();
        this.hShopStockID.Value = dr["ID"].ToString();

        string GoodPic_Big = "NoPicture.jpg";
        string GoodPic_Small = "NoPicture.jpg";

        StringBuilder sb = new StringBuilder();

        //图片列表中的图片
        DataTable dt2 = new DAL.Tables.tbl_Shop_GoodPic().Open("", "IsShow = 1 and GoodID=" + Shove._Convert.StrToLong(dr["GoodID"].ToString(), 0), "");
        if (dt != null && dt2.Rows.Count > 0)
        {
            if (dt2.Rows[0]["GoodPic_Big"].ToString() != "")
            {
                GoodPic_Big = dt2.Rows[0]["GoodPic_Big"].ToString(); 
            }
            if (dt2.Rows[0]["GoodPic_Small"].ToString() != "")
            {
                GoodPic_Small = dt2.Rows[0]["GoodPic_Small"].ToString();
            }

            sb.AppendLine("<a href='" + GoodPic_Big + "' class='jqzoom' title=''><img src='" + GoodPic_Small + "' alt='' id='current_img'></a>");

            StringBuilder = "<ul id='image_list' class='jcarousel-skin-tango'>";
            foreach (DataRow dr2 in dt2.Rows)
            {
                GoodPic_Big = "NoPicture.jpg";
                GoodPic_Small = "NoPicture.jpg";

                if (dr2["GoodPic_Big"].ToString() != "")
                {
                    GoodPic_Big = dr2["GoodPic_Big"].ToString();
                }
                if (dr2["GoodPic_Small"].ToString() != "")
                {
                    GoodPic_Small = dr2["GoodPic_Small"].ToString();
                }

                StringBuilder += "<li><img src='" + GoodPic_Small + "' alt='" + GoodPic_Big + "' /></li>";
            }
            StringBuilder += "</ul>";
        }
        else
        {
            sb.AppendLine("<img src='" + "UpFile/Goods/" + dr["GoodPic_Small"].ToString() + "' alt='' id='current_img'></a>");
        }

        lbZoom.Text = sb.ToString();
    }

    /// <summary>
    /// 绑定商品评论
    /// </summary>
    private void BindGoodComment()
    {
        DataTable dt = null;

        dt = new DAL.Views.V_Site_GoodComment().Open("", "ShopStockID=" + this.hShopStockID.Value + " and Type = 2", "");

        if (dt != null && dt.Rows.Count > 0)
        {
            dlComment.DataSource = dt;
            dlComment.DataBind();
        }
    }

    /// <summary>
    /// 绑定商品咨询
    /// </summary>
    private void BindGoodConsult()
    {
        DataTable dt = null;

        dt = new DAL.Views.V_Site_GoodComment().Open("", "ShopStockID=" + this.hShopStockID.Value + " and Type = 1", "");

        if (dt != null && dt.Rows.Count > 0)
        {
            dlConsult.DataSource = dt;
            dlConsult.DataBind();
        }
    }

    /// <summary>
    /// 商品评论
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOK_Click(object sender, EventArgs e)
    {
        DAL.Tables.tbl_Site_GoodComment GoodComment = new DAL.Tables.tbl_Site_GoodComment();
        GoodComment.ShopStockID.Value = Shove._Convert.StrToLong(this.hShopStockID.Value, 0);
        GoodComment.CustomerID.Value = _Customer.ID;
        GoodComment.Title.Value = txtTitle.Text;
        GoodComment.Content.Value = txtContent.Text;
        GoodComment.Type.Value = 2;
        GoodComment.ShopID.Value = _Site.ID;
        GoodComment.Insert();

        txtTitle.Text = "";
        txtContent.Text = "";
        Response.Redirect("Default.aspx?PN=goods_details&ID=" + this.hID.Value + "");
    }

    /// <summary>
    /// 商品咨询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCommit_Click(object sender, EventArgs e)
    {
        DAL.Tables.tbl_Site_GoodComment GoodComment = new DAL.Tables.tbl_Site_GoodComment();
        GoodComment.ShopStockID.Value = Shove._Convert.StrToLong(this.hShopStockID.Value, 0);
        GoodComment.CustomerID.Value = _Customer.ID;
        GoodComment.Title.Value = txtConsultTitle.Text;
        GoodComment.Content.Value = txtConsultContent.Text;
        GoodComment.Type.Value = 1;
        GoodComment.ShopID.Value = _Site.ID;
        GoodComment.Insert();

        txtTitle.Text = "";
        txtContent.Text = "";
        Response.Redirect("Default.aspx?PN=goods_details&ID=" + this.hID.Value + "");
    }

    /// <summary>
    /// 商品收藏
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibFav_Click1(object sender, ImageClickEventArgs e)
    {
        if (_Customer == null)
        {
            Response.Redirect("Default.aspx?PN=Login");
        }
        else
        {
            long ShopID = _Site.ID;

            DataTable dt = new DAL.Tables.tbl_Site_FatoviteGood().Open("", "ShopID=" + ShopID + " and CustomerID=" + _Customer.ID + "", "");
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["GoodID"].ToString() == this.hID.Value)
                    {
                        Shove._Web.JavaScript.Alert(this.Page, "此商品已在你的个人收藏中！");
                        return;
                    }
                }
            }

            DAL.Tables.tbl_Site_FatoviteGood f = new DAL.Tables.tbl_Site_FatoviteGood();
            f.CustomerID.Value = _Customer.ID;
            f.ShopID.Value = ShopID;
            f.GoodID.Value = Shove._Convert.StrToLong(this.hID.Value, 0);
            f.Insert();

            Shove._Web.JavaScript.Alert(this.Page, "收藏成功！");
        }
    }

    protected void dlConsult_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            Label l = (Label)e.Item.FindControl("lbReplyDate");
            if (l.Text == "")
            {
                l.Text = "商店暂无回复！";
            }
            else
            {
                l.Text = "商店于" + l.Text + "回复";
            }

            Label content = (Label)e.Item.FindControl("lbReplyContent");
            if (content.Text != "")
            {
                content.Text = "回复内容：" + content.Text;
            }
        }
    }
}
